package com.lebang.controller;


import com.lebang.common.Login;
import com.lebang.service.SysCommonService;
import com.lebang.service.TaskScreenshotService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author admin
 */
@Controller
public class SysCommonController {

    @Autowired
    private SysCommonService commonService;
    @Autowired
    private TaskScreenshotService taskScreenshotService;

    @Login
    @RequestMapping(value = ("/layui/list/data"), method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> datalist(HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> list = new HashMap<>();
        /**
         * 参数列表
         * */
        Map<String, Object> paramsMap = queryParameter(request);

        int pageSize = Integer.parseInt(String.valueOf(paramsMap.get("limit")));
        int page = Integer.parseInt(String.valueOf(paramsMap.get("page")));
        int startNum = (page - 1) * pageSize;
        String table = (String) paramsMap.get("table");
        Map<String, String> queryMap = (Map<String, String>) paramsMap.get("query");
        Map<String, String> timeMap = (Map<String, String>) paramsMap.get("time");
        Map<String, String> searchMap = (Map<String, String>) paramsMap.get("key");
        String orderBy = paramsMap.get("orderBy") == null ? "" : paramsMap.get("orderBy").toString();

        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", commonService.listByQuery(table, pageSize, startNum, queryMap, timeMap, searchMap, orderBy));
        returnMap.put("count", commonService.countByQuery(table, queryMap, timeMap, searchMap));
        returnMap.put("code", 0);
        return returnMap;
    }

    public Map<String, Object> queryParameter(HttpServletRequest request) {
        /**
         * 主要的查询参数，包含
         * limit 每页数量
         * page  当前页
         * table 表名
         * query 查询的字段名称和值
         * time  查询的时间段
         * */
        Map<String, Object> resultMap = new HashMap<>();

        /**
         * 获取and 的查询条件
         * */
        Map<String, String> queryMap = new HashMap<>();
        /**
         * 获取时间的查询条件，针对于修改做操作 和修改时间做比较
         * */
        Map<String, Object> timeMap = new HashMap<>();

        /**
         *  获取查询检索条件，or
         * */
        Map<String, String> searchMap = new HashMap<>();


        Map<String, String[]> paramMap = request.getParameterMap();
        Iterator<String> iterator = paramMap.keySet().iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            String[] val = paramMap.get(key);
            if (val != null && val.length > 0) {
                String value = val[0].trim();
                if (!TextUtil.isEmpty(key, value)) {
                    String str = "";
                    if (key.contains("query")) {
                        String name = key.substring(key.indexOf("[") + 1, key.indexOf("]"));
                        queryMap.put(name, value);
                    } else if (key.contains("time")) {
                        String name = key.substring(key.indexOf("[") + 1, key.indexOf("]"));
/*                        if("end".equals(name)){
                            value= this.getNowAfter(value);
                        }*/
                        timeMap.put(name, value);
                    } else if (key.contains("key")) {
                        String name = key.substring(key.indexOf("[") + 1, key.indexOf("]"));
                        String[] names = name.split("[,]");
                        for (String string : names) {
                            searchMap.put(string, value);
                        }
                    } else {
                        resultMap.put(key, value);
                    }
                }
            }
        }

        resultMap.put("query", queryMap);
        resultMap.put("time", timeMap);
        resultMap.put("key", searchMap);
        return resultMap;
    }

    @Login
    @RequestMapping(value = ("/delete/data"), method = RequestMethod.POST)
    @ResponseBody
    public JsonData deletedData(HttpServletRequest request, HttpServletResponse response, String table, String ids) {
        if (TextUtil.isEmpty(table, ids)) {
            return Callback.fail("500", "数据异常", "");
        }
        boolean isSuccess = commonService.deletedData(table, ids);
        if (isSuccess) {
            return Callback.success("200", "操作成功", "");
        }
        return Callback.fail("500", "网络异常", "");
    }

    @Login
    @RequestMapping(value = "/common/enablebyid", method = RequestMethod.POST)
    @ResponseBody
    public JsonData enablebyid(Integer id, String status, String table) {

        return commonService.enablebyid(id, status, table);
    }
    @Login
    @RequestMapping(value = "/common/commonSwitch", method = RequestMethod.POST)
    @ResponseBody
    public JsonData commonSwitch(Integer id, String status, String table, String feild) {

        return commonService.commonSwitch(id, status, table, feild);
    }
    @Login
    @GetMapping("/menutest")
    public String menutest() {
        return "common/menutest";
    }
    @Login
    @GetMapping("/sys/config/list")
    public String config() {
        return "/config/list";
    }

    /**
     * 首页展示配置上架任务
     * @param request
     * @param title
     * @return
     */
    @Login
    @RequestMapping(value = ("/common/get/task"), method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> datalist(HttpServletRequest request, String title) {
        Map<String, Object> paramsMap = queryParameter(request);
        int pageSize = Integer.parseInt(String.valueOf(paramsMap.get("limit")));
        int page = Integer.parseInt(String.valueOf(paramsMap.get("page")));
        int startNum = (page - 1) * pageSize;
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", taskScreenshotService.selectByTitle(title,startNum,pageSize));
        returnMap.put("count",taskScreenshotService.countByTitle(title));
        returnMap.put("code", 0);
        return returnMap;
    }

}
